package com.tyqx.hashchainbiz.controller.jzt;

import com.tyqx.hashchainbean.module.UserNameAuthorityQuery;
import com.tyqx.hashchainbean.vo.JztLogisticsCountVO;
import com.tyqx.hashchainbean.vo.UserNameAuthorityQueryVO;
import com.tyqx.hashchainbiz.service.UserNameAuthorityQueryService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/JZTApi/authority")
@CrossOrigin
public class UserNameAuthorityQueryController {

    @Resource
    private UserNameAuthorityQueryService userNameAuthorityQueryService;

    @RequestMapping(value = "/addLogisticsAuthorityQuery", method = RequestMethod.POST)
    @ResponseBody
    public Dto addLogisticsAuthorityQuery(@RequestParam(value = "userName", required = true)String userName,@RequestParam(value = "grantUserName", required = true)String grantUserName) throws Exception {

        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = sdf.parse(sdf.format(currentDate));
        Date updateTime = createTime;

        String grantType="logistics";

        List<Integer> flag=userNameAuthorityQueryService.selectIdByUserNameAndGrantUserName(userName,grantUserName,grantType);
        if(flag.size()>0){
            return  DtoUtil.returnSuccess("已添加,请更换授权人");
        }else{
            UserNameAuthorityQuery userNameAuthorityQuery=new UserNameAuthorityQuery();
            userNameAuthorityQuery.setCreateTime(createTime);
            userNameAuthorityQuery.setUpdateTime(updateTime);
            userNameAuthorityQuery.setUserName(userName);
            userNameAuthorityQuery.setGrantType(grantType);
            userNameAuthorityQuery.setGrantUserName(grantUserName);

            Integer flag1=userNameAuthorityQueryService.addUserNameAuthorityQuery(userNameAuthorityQuery);

            if(flag1>0){
                return  DtoUtil.returnSuccess("添加成功");
            }else{
                return DtoUtil.returnFail("添加失败","100001");
            }
        }


    }

    @RequestMapping(value = "/deleteLogisticsAuthorityQuery", method = RequestMethod.POST)
    @ResponseBody
    public Dto deleteLogisticsAuthorityQuery(@RequestParam(value = "userName", required = true)String userName,@RequestParam(value = "grantUserName", required = true)String grantUserName) throws Exception {


        String grantType="logistics";

        List<Integer> idList=userNameAuthorityQueryService.selectIdByUserNameAndGrantUserName(userName,grantUserName,grantType);
        if(idList.size()>0){
            Integer count=0;
            for (int i=0;i<idList.size();i++){
                Integer id=idList.get(i);
                Integer flag=userNameAuthorityQueryService.deleteUserNameAuthorityQuery(id);
                count+=flag;
            }
            if(count>0){
                return DtoUtil.returnSuccess("删除成功");
            }else {
                return DtoUtil.returnSuccess("删除失败");
            }
        }else{
            return DtoUtil.returnFail("没有找到删除的数据","100001");
        }

    }

    @RequestMapping(value = "/addCodeCirculationAuthorityQuery", method = RequestMethod.POST)
    @ResponseBody
    public Dto addCodeCirculationAuthorityQuery(@RequestParam(value = "userName", required = true)String userName,@RequestParam(value = "grantUserName", required = true)String grantUserName) throws Exception {

        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = sdf.parse(sdf.format(currentDate));
        Date updateTime = createTime;

        String grantType="codeCirculation";

        List<Integer> flag=userNameAuthorityQueryService.selectIdByUserNameAndGrantUserName(userName,grantUserName,grantType);
        if(flag.size()>0){
            return  DtoUtil.returnSuccess("已添加,请更换授权人");
        }else {
            UserNameAuthorityQuery userNameAuthorityQuery = new UserNameAuthorityQuery();
            userNameAuthorityQuery.setCreateTime(createTime);
            userNameAuthorityQuery.setUpdateTime(updateTime);
            userNameAuthorityQuery.setUserName(userName);
            userNameAuthorityQuery.setGrantType(grantType);
            userNameAuthorityQuery.setGrantUserName(grantUserName);

            Integer flag1 = userNameAuthorityQueryService.addUserNameAuthorityQuery(userNameAuthorityQuery);

            if (flag1 > 0) {
                return DtoUtil.returnSuccess("添加成功");
            } else {
                return DtoUtil.returnFail("添加失败", "100001");
            }
        }
    }

    @RequestMapping(value = "/deleteCodeCirculationAuthorityQuery", method = RequestMethod.POST)
    @ResponseBody
    public Dto deleteCodeCirculationAuthorityQuery(@RequestParam(value = "userName", required = true)String userName,@RequestParam(value = "grantUserName", required = true)String grantUserName) throws Exception {


        String grantType="codeCirculation";

        List<Integer> idList=userNameAuthorityQueryService.selectIdByUserNameAndGrantUserName(userName,grantUserName,grantType);
        if(idList.size()>0){
            Integer count=0;
            for (int i=0;i<idList.size();i++){
                Integer id=idList.get(i);
                Integer flag=userNameAuthorityQueryService.deleteUserNameAuthorityQuery(id);
                count+=flag;
            }
            if(count>0){
                return DtoUtil.returnSuccess("删除成功");
            }else {
                return DtoUtil.returnSuccess("删除失败");
            }
        }else{
            return DtoUtil.returnFail("没有找到删除的数据","100001");
        }

    }

    @RequestMapping(value = "/addGS1AuthorityQuery", method = RequestMethod.POST)
    @ResponseBody
    public Dto addGS1AuthorityQuery(@RequestParam(value = "userName", required = true)String userName,@RequestParam(value = "grantUserName", required = true)String grantUserName) throws Exception {

        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = sdf.parse(sdf.format(currentDate));
        Date updateTime = createTime;

        String grantType="gs1";
        List<Integer> flag=userNameAuthorityQueryService.selectIdByUserNameAndGrantUserName(userName,grantUserName,grantType);
        if(flag.size()>0){
            return  DtoUtil.returnSuccess("已添加,请更换授权人");
        }else {
            UserNameAuthorityQuery userNameAuthorityQuery = new UserNameAuthorityQuery();
            userNameAuthorityQuery.setCreateTime(createTime);
            userNameAuthorityQuery.setUpdateTime(updateTime);
            userNameAuthorityQuery.setUserName(userName);
            userNameAuthorityQuery.setGrantType(grantType);
            userNameAuthorityQuery.setGrantUserName(grantUserName);

            Integer flag1 = userNameAuthorityQueryService.addUserNameAuthorityQuery(userNameAuthorityQuery);

            if (flag1 > 0) {
                return DtoUtil.returnSuccess("添加成功");
            } else {
                return DtoUtil.returnFail("添加失败", "100001");
            }
        }
    }

    @RequestMapping(value = "/deleteGS1AuthorityQuery", method = RequestMethod.POST)
    @ResponseBody
    public Dto deleteGS1AuthorityQuery(@RequestParam(value = "userName", required = true)String userName,@RequestParam(value = "grantUserName", required = true)String grantUserName) throws Exception {


        String grantType="gs1";

        List<Integer> idList=userNameAuthorityQueryService.selectIdByUserNameAndGrantUserName(userName,grantUserName,grantType);
        if(idList.size()>0){
            Integer count=0;
            for (int i=0;i<idList.size();i++){
                Integer id=idList.get(i);
                Integer flag=userNameAuthorityQueryService.deleteUserNameAuthorityQuery(id);
                count+=flag;
            }
            if(count>0){
                return DtoUtil.returnSuccess("删除成功");
            }else {
                return DtoUtil.returnSuccess("删除失败");
            }
        }else{
            return DtoUtil.returnFail("没有找到删除的数据","100001");
        }

    }

    @RequestMapping(value = "/queryUserNameAuthorityQueryByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto queryUserNameAuthorityQueryByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        List<UserNameAuthorityQueryVO> userNameAuthorityQueryList=userNameAuthorityQueryService.queryUserNameAuthorityQueryByUserName(userName);
        if(userNameAuthorityQueryList.size()>0){
            return DtoUtil.returnSuccess("查询成功",userNameAuthorityQueryList);
        }else{
            return DtoUtil.returnFail("未查询到记录","100001");
        }

    }

}
